package cn.tedu.service;

import cn.tedu.mapper.CommentMapper;
import cn.tedu.pojo.Comment;
import cn.tedu.vo.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:努力学java的小雨
 * @创建时间:2021/9/15 9:32
 */
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public List<Comment> findAll() {
        return commentMapper.selectList(null);
    }

    @Override
    public PageResult getComment(PageResult pageResult) {
        IPage<Comment> page = new Page<>();
        page.setCurrent(pageResult.getPageNum()).setSize(pageResult.getPageSize());
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        boolean flag= StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "content", pageResult.getQuery());
        page = commentMapper.selectPage(page, queryWrapper);
        pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
        return pageResult;
    }

    @Override
    public void postComment(Comment comment) {
        String date = new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date());
        comment.setCreatedtime(date);
        commentMapper.insert(comment);
    }

    @Override
    public void deleteComment(Integer id) {
        commentMapper.deleteById(id);
    }
}
